use crate::config::AppConfig;
use crate::error::Result;
use crate::models::Claims;
use chrono::{Duration, Utc};
use jsonwebtoken::{encode, EncodingKey, Header};

pub fn create_token(email: &str, config: &AppConfig) -> Result<String> {
    let expiration = Utc::now() + Duration::minutes(config.auth.jwt_expiration_minutes as i64);

    let claims = Claims {
        sub: email.to_string(),
        exp: expiration.timestamp() as usize,
        iat: Utc::now().timestamp() as usize,
    };

    let token = encode(
        &Header::default(),
        &claims,
        &EncodingKey::from_secret(config.auth.jwt_secret_key.as_ref()),
    )?;

    Ok(token)
}
